package applet;

import java.util.*;

import processing.core.PApplet;

public class PieChart {

	float xPos, yPos;
	float diameter;
	ArrayList<ChartElement> pieElements;
//	boolean legend;		//display legend if true
	int elementCount;
	int maxCount;
	float sum;
	float startAngle, stopAngle;
	int[] color;
	
	PieChart(float x, float y, float d, ArrayList<ChartElement> pe) {//, boolean bool) {
		xPos = x;
		yPos = y;
		diameter = d;
//		legend = bool;
		maxCount = 10;
		pieElements = sortAndResize(pe);
		elementCount = pieElements.size();
		sum = 0;
		for(ChartElement p: pieElements) {
			sum+=p.number;
		}
		PApplet.println("Sum is: "+sum);
		color = new int[10];
		color[0] = GLOBAL.processing.color(0x56,0x79,0xC1);
		color[1] = GLOBAL.processing.color(0xD1,0x16,0x49);
		color[2] = GLOBAL.processing.color(0x79,0x79,0x79);//#79C156;
		color[3] = GLOBAL.processing.color(0x59,0xC6,0x71);
		color[4] = GLOBAL.processing.color(0xBF,0xBF,0x5D);
		color[5] = GLOBAL.processing.color(0x71,0x59,0xC6);
		color[6] = GLOBAL.processing.color(0xC6,0x71,0x59);
		color[7] = GLOBAL.processing.color(0x79,0xC1,0x56);
		color[8] = GLOBAL.processing.color(0xE1,0x39,0xA1);
		color[9] = GLOBAL.processing.color(0xBA,0xBA,0xBA);
		

	}
	
	ArrayList<ChartElement> sortAndResize(ArrayList<ChartElement> pe ) {
		ChartElement temp = new ChartElement();
		for(int i = 1; i < pe.size(); i++){
			for(int j = 0; j< pe.size()-i; j++) {
				if(pe.get(j).number < pe.get(j+1).number) {
					temp = pe.get(j);
					pe.set(j, pe.get(j+1));
					pe.set(j+1, temp);
				}
			}
		}
		if(pe.size()>maxCount){
			float otherSum = 0;
			for(int i = maxCount-1; i <pe.size(); i++) {
				otherSum+=pe.get(i).number;
			}
			pe.set(maxCount-1,new ChartElement(otherSum,"Other"));
			for(int i = pe.size()-1; i>= maxCount; i--) {
				pe.remove(i);
			}
			PApplet.println("Other has: "+otherSum);
		}
		return pe;
	}
	
	void draw() {
//	    GLOBAL.processing.ellipseMode(PApplet.CENTER);
		startAngle = 0;
		for(int i = 0; i< elementCount; i++) {
			GLOBAL.processing.stroke(192);
			stopAngle = startAngle + 2*PApplet.PI*pieElements.get(i).number/sum;
			GLOBAL.processing.fill(color[i]);
			GLOBAL.processing.arc(xPos+diameter*0.5f, yPos+diameter*0.5f, diameter, diameter, startAngle, stopAngle);
			startAngle = stopAngle;
			GLOBAL.processing.noStroke();
			GLOBAL.processing.rect(xPos+diameter*1.05f, yPos+diameter*(0.1f*i+0.025f), 8, 8);
			GLOBAL.processing.fill(224);
			GLOBAL.processing.textFont(GLOBAL.fontTimes12,12);
			GLOBAL.processing.textAlign(PApplet.LEFT,PApplet.CENTER);
			GLOBAL.processing.text(pieElements.get(i).label+"  "+PApplet.nf((pieElements.get(i).number/sum*100), 1, 2)+"%",xPos+diameter*1.15f, yPos+diameter*(0.1f*i+0.05f));
		}

	}
	
	void setPosition(float x, float y) {
		xPos = x;
		yPos = y;
	}
	
	void setDiameter(float d) {
		diameter = d;
	}
	
	void setData(ArrayList<ChartElement> pe) {
		pieElements = sortAndResize(pe);
		elementCount = pieElements.size();
		sum = 0;
		for(ChartElement p: pieElements) {
			sum+=p.number;
		}
	}
}
